<?php
/**
 *
 * 版权所有：恰维网络<qwadmin.qiawei.com>
 * 作    者：寒川<hanchuan@qiawei.com>
 * 日    期：2016-09-20
 * 版    本：1.0.0
 * 功能说明：文章控制器。
 *
 **/

namespace Qwadmin\Controller;

use Vendor\Tree;

class FoodstuffController extends ComController
{

    public function add()
    {
        $this->display("form");
    }

    public function index($sid = 0, $p = 1)
    {

        $Foodsstuff = M("foodstuff");
        $pagesize = 20;#每页数量
        $offset = $pagesize * ($p - 1);//计算记录偏移量
        $prefix = C('DB_PREFIX');
        $where = "1=1";
        $count = $Foodsstuff->where($where)->count();
        $list = $Foodsstuff
            ->where($where)->order("o asc")
            ->limit($offset . ',' . $pagesize)->select();

        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();
        $this->assign('list', $list);
        $this->assign('page', $page);
        $this->display();
    }

    public function del(){
        $id = I("get.id");
        if ($id) {
            $map['id'] = $id;
            if (M('foodstuff')->where($map)->delete()) {
                addlog('删除粮食品种，ID：' . $id);
                $this->success('粮食品种删除成功！');
            } else {
                $this->error('参数错误！');
            }
        } else {
            $this->error('参数错误！');
        }

    }
    public function edit(){

        $id = I("get.id");
        $res = M('foodstuff')->find($id);
//        dump($res);exit;
        $this->assign("res",$res);
        $this->display('form');
    }

    public function update(){

        $Foodstuff = D("foodstuff");
        if(!$Foodstuff->create()){
            $this->error($Foodstuff->getError());
        }
        $data = I("post.");


        if(isset($data['id']) && $data['id']!=""){
            $res = $Foodstuff->data($data)->save();

            if($res!==false){
                $this->success("粮食品种编辑成功!",U("index"));
            }else{
                $this->error("粮食品种编辑失败!");
            }
        }else{
            $res = $Foodstuff->data($data)->add();
            if($res){
                $this->success("粮食品种添加成功!",U("index"));
            }else{
                $this->error("粮食品种添加失败!");
            }
        }

    }








}